package mpcs.utils
{
	import flash.utils.Dictionary;
	
	import flexlib.mdi.containers.MDIWindow;
	
	import mpcs.model.vo.GlobalVO;
	import mpcs.view.windows.GoogleMDIWindow;
	import mpcs.view.windows.HistoryMDIWindow;
	import mpcs.view.windows.MapMDIWindow;
	import mpcs.view.windows.MoresMDIWindow;
	import mpcs.view.windows.UserInfoMDIWindow;

	/**
	 * mdi窗口对象池
	 * @author zhangzuoqiang
	 */
	public class MDIWindowPools {
		
		private static var instance:MDIWindowPools;
		/**mdi窗口的对象数组**/
		private var mdiPools:Dictionary;
		/**mdi窗口的对象数组的大小**/
		private var mdiPoolsLen:int = 0;
		
		public function MDIWindowPools() {
			mdiPools = new Dictionary();
		}
		
		/**从mdiPools对象数组中获取已经创建好的MDIWindow实例，如果对象池中无可用，则新建，并设置一些基本属性**/
		public function getMDIWindow(type:String):MDIWindow {
			var newMDI:MDIWindow ;			
			switch(type) {
				case MDIWindowType.MAP_TYPE:
					if(mdiPoolsLen > 0 && mdiPools.hasOwnProperty(MDIWindowType.MAP_TYPE)){
						trace("从MDI对象池中取出一个MapMDIWindow实例，当前对象池个数= " + (mdiPoolsLen - 1) );
						mdiPoolsLen --;
						newMDI = mdiPools[MDIWindowType.MAP_TYPE];
						updateMDIValues(newMDI);
					}else{
						trace("当前对象池为空, 通过对象池方法创建一个MapMDIWindow实例 ... ");
						newMDI = new MapMDIWindow();
						/////////////////////////////////////////////////////////////////////////////////////////
						if( MapManager.googleMap != null ){
							trace("MDIWindowPools ... 添加地图进MapMDIWindow ... ");
//							newMDI
						}
						updateMDIValues(newMDI);
					}
					break;
				case MDIWindowType.USERINFO_TYPE:
					if(mdiPoolsLen > 0 && mdiPools.hasOwnProperty(MDIWindowType.USERINFO_TYPE)){
						trace("从MDI对象池中取出一个UserInfoMDIWindow实例，当前对象池个数= " + (mdiPoolsLen - 1) );
						mdiPoolsLen --;
						newMDI = mdiPools[MDIWindowType.USERINFO_TYPE];
						updateMDIValues(newMDI);
					}else{
						trace("当前对象池为空, 通过对象池方法创建一个UserInfoMDIWindow实例 ... ");
						newMDI = new UserInfoMDIWindow();
						updateMDIValues(newMDI);
					}
					break;
				/////////////////////////////////////////////////////////////////////////////////////////////////////////////
				case MDIWindowType.HISTORY_TYPE:
					if(mdiPoolsLen > 0 && mdiPools.hasOwnProperty(MDIWindowType.HISTORY_TYPE)){
						trace("从MDI对象池中取出一个HistoryMDIWindow实例，当前对象池个数= " + (mdiPoolsLen - 1) );
						mdiPoolsLen --;
						newMDI = mdiPools[MDIWindowType.HISTORY_TYPE];
						updateMDIValues(newMDI);
					}else{
						trace("当前对象池为空, 通过对象池方法创建一个HistoryMDIWindow实例 ... ");
						newMDI = new HistoryMDIWindow();
						updateMDIValues(newMDI);
					}
					break;
				case MDIWindowType.MORESERVICE_TYPE:
					if(mdiPoolsLen > 0 && mdiPools.hasOwnProperty(MDIWindowType.MORESERVICE_TYPE)){
						trace("从MDI对象池中取出一个MoresMDIWindow实例，当前对象池个数= " + (mdiPoolsLen - 1) );
						mdiPoolsLen --;
						newMDI = mdiPools[MDIWindowType.MORESERVICE_TYPE];
						updateMDIValues(newMDI);
					}else{
						trace("当前对象池为空, 通过对象池方法创建一个MoresMDIWindow实例 ... ");
						newMDI = new MoresMDIWindow();
						updateMDIValues(newMDI);
					}
					break;
				case MDIWindowType.GOOGLE_TYPE:
					if(mdiPoolsLen > 0 && mdiPools.hasOwnProperty(MDIWindowType.GOOGLE_TYPE)){
						trace("从MDI对象池中取出一个GoogleMDIWindow实例，当前对象池个数= " + (mdiPoolsLen - 1) );
						mdiPoolsLen --;
						newMDI = mdiPools[MDIWindowType.GOOGLE_TYPE];
						updateMDIValues(newMDI);
					}else{
						trace("当前对象池为空, 通过对象池方法创建一个GoogleMDIWindow实例 ... ");
						newMDI = new GoogleMDIWindow();
						updateMDIValues(newMDI);
					}
					break;
			}
			return newMDI;
		}
		
		/**更新MDI实例的参数**/
		private function updateMDIValues(mdi:MDIWindow):void {
			mdi.width = 720;
			mdi.height = 500;
			mdi.x = 200;
			mdi.y = 30;
			
			mdi.titleIcon = GlobalVO.logoIcon;
		}
		
		/**返回遗弃的对象到对象池，可以在此方法中清空MDIWindow实例的一些属性**/
		public function return2Pools(type:String, window:MDIWindow):void{
			mdiPoolsLen ++;
			mdiPools[type] = window;	
			switch(type) {
				case MDIWindowType.MAP_TYPE:
					trace("===== MapMDIWindow实例回收成功，当前对象池中有 " + mdiPoolsLen + " 个MDIWindow对象实例 ...");
					break;
				case MDIWindowType.USERINFO_TYPE:
					trace("===== UserInfoMDIWindow实例回收成功，当前对象池中有 " + mdiPoolsLen + " 个MDIWindow对象实例 ...");
					break;
				case MDIWindowType.HISTORY_TYPE:
					trace("===== HistoryMDIWindow实例回收成功，当前对象池中有 " + mdiPoolsLen + " 个MDIWindow对象实例 ...");
					break;
				case MDIWindowType.MORESERVICE_TYPE:
					trace("===== MoresMDIWindow实例回收成功，当前对象池中有 " + mdiPoolsLen + " 个MDIWindow对象实例 ...");
					break;
				case MDIWindowType.GOOGLE_TYPE:
					trace("===== GoogleMDIWindow实例回收成功，当前对象池中有 " + mdiPoolsLen + " 个MDIWindow对象实例 ...");
					break;
			}			
		}
		
		/**获取该对象池的实例，采用单例模式**/
		public static function getInstance():MDIWindowPools{
			if(!instance)
				instance = new MDIWindowPools();
			return instance;
		}
		
	}
}